import Vue, { nextTick } from 'vue'
import Dialog from './Index.vue'

const DialogConstructor = Vue.extend(Dialog)
let dialog = null
let el = null
let mask = null

function showDialog({ title, content, btns }) {
  const data = {
    show: true,
    title,
    btns,
    content,
  }

  btns.forEach(btn => {
    const handler = btn.handler.bind(btn)

    btn.handler = function() {
      handler(arguments)
      data.show = false
    }
  })

  dialog = new DialogConstructor({
    data() {
      return data
    }
  })


  el = dialog.$mount().$el
  document.body.appendChild(el)
}

export default {
  install(Vue) {
    Vue.prototype.$dialog = showDialog
  }
}
